import { Component, OnInit } from '@angular/core';

import { ModalController, NavParams } from '@ionic/angular';

import { table } from 'console';

import { element } from 'protractor';

import { HttpService } from 'src/app/shared/service/http.service';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';

import { ModalOfPersonSearchComponent } from '../modal-of-person-search/modal-of-person-search.component';
@Component({
  selector: 'app-modal-of-person-select',
  templateUrl: './modal-of-person-select.component.html',
  styleUrls: ['./modal-of-person-select.component.scss'],
})
export class ModalOfPersonSelectComponent implements OnInit {
  data: any;
  itemList: any;
  selectItem: any;
    // 当前页数，默认1
  pageNo = 1;
    // size
  pageSize: number;
    // 数量
  pageTotal: number;
  constructor(private modalCtrl: ModalController,
              private navParams: NavParams,
              private http: HttpService,
              private appGlobalService: AppGlobalService) { }


  ngOnInit() {

    this.data = this.navParams.data.value;

    if (this.data.submit) {
      this.getData(null, null);
    } else {
    // 测试 直接传数据来的
    // this.itemList = this.data;
    }

  }

  getData(keyWord?, event?) {
    // TODO: check  content.value
    this.pageNo = this.pageNo < 1 ?  1 : this.pageNo;

    let submitData: any = {};
    if (keyWord) {
      this.data.modal.research.value = keyWord;
      this.pageNo = 1;

    } else {
      // delete this.data.modal.research.value;
    }


    submitData = {
      sessionId: localStorage.getItem('sessionId'),
      id: this.data.submit.id,
      content : {
        modal: {
          display: this.data.modal.display,
          esTable: this.data.modal.esTable,
        }
      }
    };

    submitData.content.modal.esTable.pageNo = this.pageNo;
    if (keyWord) {
      submitData.content.modal.research = this.data.modal.research;
    }
      // 请求获取新数据
    this.http.post('application/submit', submitData, res => {
        // 请求数据成功
        if (res.data && res.data.dataList.length) {
          console.log('res.data.dataList');
            // 追加返回数据
          if (this.pageNo > 1) {
              // 追加数据
              res.data.dataList.map(item => {
                item.check = false;
                this.itemList.push(item);
              });
          } else {
            this.itemList = res.data.dataList;
            this.itemList.forEach(element => {
              element.check = false;
              if (this.data.selectValue && element.value === this.data.selectValue) {
                element.check = true;
                this.selectItem = element;
              }
            });
          }

        } else if (res.success) {
          if (this.pageNo > 1) {
              // 没有数据了
              // this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
              event.target.disable = true;
              this.pageNo -= 1;
          } else {
            // this.appGlobalService.showToast('暂无数据...', 1000, 'middle');
            this.pageNo = 1;
            this.itemList = [];
          }
        } else {
            // 请求数据失败
            this.appGlobalService.showToast(res.message, 2000, 'middle');
            this.pageNo -= 1;
          }
        event ? event.target.complete() : null;


          // this.demoData();}
      },
       err => {
        // 请求失败，更新当前页数
        this.pageNo = this.pageNo > 1 ? this.pageNo - 1 : 1;
        // 请求数据失败
        this.appGlobalService.showToast(err.message, 2000, 'middle');
        event ? event.target.complete() : null ;

      });

  }
  demoData() {
    this.itemList = [
      {
        label: "深圳王1",
        value: 1, // userId ？
        tagLabel: '深圳总部1',
        otherPhone: '1357999999',
        phone: '1357999999',
        check: false
      },
      {
        label: "深圳王2",
        value: 2, // userId ？
        tagLabel: '深圳总部2',
        otherPhone: '1357999999',
        phone: '1357999999',
        check: false
      },
      {
        label: "深圳王3",
        value: 3, // userId ？
        tagLabel: '深圳总部3',
        otherPhone: '1357999999',
        phone: '1357999999',
        check: false
      },
    ];
    this.itemList.forEach(element => {
      element.check = false;
      if (this.data.selectValue && element.value === this.data.selectValue) {
        element.check = true;
        this.selectItem = element;

      }
    });
  }

  cancel() {
    this.modalCtrl.dismiss();
  }
  // 打开搜索人员模态框
  async searchButtonClick() {
  const that = this;
  const modal = await this.modalCtrl.create({
    component: ModalOfPersonSearchComponent,
    componentProps: { value: {sarchPerson: true} }
  });
  modal.onDidDismiss().then(function(data) {
    if (data.data && data.data.data) {
      const keyWord = data.data.data;
      console.log(keyWord);
      that.getData(keyWord, null);

    }

  });
  return await modal.present();
}

  sureClick() {
    if (this.selectItem)  {
      this.modalCtrl.dismiss({ data: this.selectItem});
    } else {
      this.appGlobalService.showToast('请选择人员', 1000 , 'middle');
    }
  }
  itemClick(item) {
    if (item.check) {
      this.selectItem = item;
    } else {
      this.itemList.forEach(element => {
        if (element === item) {
          item.check = true;
          this.selectItem = item;
        } else {
          element.check = false;
        }
      });
    }
  }

    loadMoreData(event) {
      // if (this.pageNo >= this.pageTotal) {
      //   this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
      //   // event.complete();
      //   event.target.disable = true;
      //   return;
      // }
      // // 请求下一个页面
      this.pageNo = this.pageNo + 1;
      this.getData(null, event);

  }
}
